package net.qiujuer.sample.genius.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.NestedScrollView;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import net.qiujuer.genius.ui.drawable.LoadingCircleDrawable;
import net.qiujuer.genius.ui.drawable.LoadingDrawable;
import net.qiujuer.genius.ui.utils.ToastUtil;
import net.qiujuer.genius.ui.widget.AbsSeekBar;
import net.qiujuer.genius.ui.widget.CheckBoxAndTextField;
import net.qiujuer.genius.ui.widget.Loading;
import net.qiujuer.genius.ui.widget.SeekBar;
import net.qiujuer.sample.genius.BlurAbility;
import net.qiujuer.sample.genius.BlurClipAbility;
import net.qiujuer.sample.genius.KitAbility;
import net.qiujuer.sample.genius.ResourceTable;
import net.qiujuer.sample.genius.SeekBarAbility;
import net.qiujuer.sample.genius.bar.Toolbar;
import net.qiujuer.sample.genius.util.JumpUtil;

import java.io.IOException;

/**
 * MainAbilitySlice
 *
 * @since 2021-05-09
 */
public class MainAbilitySlice extends AbilitySlice {
    private final int LEFT_SCROLL_Y = 1184;
    private final int RIGHT_SCROLL_Y = 1334;
    private final int DELAY = 260;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_colorTitle));
        super.setUIContent(ResourceTable.Layout_ability_main);

        // (∩_∩)
        ((AbsSeekBar) findComponentById(ResourceTable.Id_AbsSeekBar4)).setIndicatorTextFormat("(∩_∩)${PROGRESS}");
        findComponentById(ResourceTable.Id_btn_submit).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ToastUtil.showToastB0ttom(getContext(), " OnClickListener.", new Color(getContext().getColor(ResourceTable.Color_white_alpha_192)),
                    new Color(getContext().getColor(ResourceTable.Color_black_alpha_80)) , 13);

            }
        });
        initScrollView();
        initToolbar();
        initLoading();
    }

    private void initScrollView() {
        NestedScrollView nestedScrollView = (NestedScrollView) findComponentById(ResourceTable.Id_nested_scroll_view);

        CheckBoxAndTextField checkBoxAndTextField = (CheckBoxAndTextField) findComponentById(ResourceTable.Id_checkbox_and_text_field);
        checkBoxAndTextField.setTextFieldFocusedListener(new CheckBoxAndTextField.TextFieldFocusedListener() {
            @Override
            public void leftTextFieldFocused(boolean isFocused) {
                if (isFocused) {
                    getUITaskDispatcher().delayDispatch(new Runnable() {
                        @Override
                        public void run() {
                            nestedScrollView.fluentScrollTo(0 , LEFT_SCROLL_Y);
                        }
                    }, DELAY);
                }
            }

            @Override
            public void rightTextFieldFocused(boolean isFocused) {
                if (isFocused) {
                    getUITaskDispatcher().delayDispatch(new Runnable() {
                        @Override
                        public void run() {
                            nestedScrollView.fluentScrollTo(0, RIGHT_SCROLL_Y);
                        }
                    }, DELAY);
                }
            }
        });
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findComponentById(ResourceTable.Id_toolbar);
        toolbar.setTitle("Genius");
        toolbar.createMenu();
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(Text item) {
                int id = item.getId();
                if (id == ResourceTable.Id_action_blur) {
                    JumpUtil.jumpToAbility(getContext(), BlurAbility.class);
                } else if (id == ResourceTable.Id_action_blur_clip) {
                    JumpUtil.jumpToAbility(getContext(), BlurClipAbility.class);
                } else if (id == ResourceTable.Id_action_kit) {
                    JumpUtil.jumpToAbility(getContext(), KitAbility.class);
                } else if (id == ResourceTable.Id_action_seekBar) {
                    JumpUtil.jumpToAbility(getContext(), SeekBarAbility.class);
                }
                return true;
            }
        });
    }

    private void initLoading() {
        final Loading loading = (Loading) findComponentById(ResourceTable.Id_loading_progress);
        loading.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Loading l = (Loading) component;
                if (l.isRunning()) {
                    l.stop();
                } else {
                    l.start();
                }
            }
        });
        //initCheckBox();
        Text tv = (Text) findComponentById(ResourceTable.Id_txt_loading);
        LoadingDrawable drawable = new LoadingCircleDrawable(tv);
        drawable.setBackgroundColor(Color.getIntColor("#bebebe"));
        int colors[] = getArrayElement(ResourceTable.Strarray_g_default_loading_fg);
        drawable.setForegroundColor(colors);
        drawable.setBackgroundLineSize(2);
        drawable.setForegroundLineSize(4);
        tv.setBackground(drawable);
        drawable.start();
    }

    private int[] getArrayElement(int resId) {
        try {
            String[] taColor = getResourceManager().getElement(resId).getStringArray();
            int length = taColor.length;
            int[] fgColorArray = new int[length];
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    fgColorArray[i] = Color.getIntColor(taColor[i]);
                }
            }
            return fgColorArray;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
